<?php
/**
 * @desc 素材下载管理
 * @author ljm
 * @date 2019-07-14
 */
namespace App\Http\Controllers\Admin;

use App\Models\DownloadCat;
use App\Models\Images;
use App\Models\Material;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MaterialController extends Controller
{
    /**
     * @var Material
     */
    public $materialModel;
    public $downCatModel;
    protected $imagesModel;

    /**
     * MaterialController constructor.
     */
    public function __construct()
    {
        $this->materialModel = new Material();
        $this->downCatModel = new DownloadCat();
        $this->imagesModel = new Images();
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view('admin.material.index');
    }

    /**
     *
     */
    public function getList(Request $request)
    {
        $params = [
            'where' => [['is_delete', 1]],
            'getfield' => true,
            'order' => 'created_at',
            'field' => ['id', 'title', 'serial', 'sizeorresol', 'file_type', 'file_size', 'images', 'price', 'is_check', 'status', 'created_at', 'updated_at'],
            'page' => $request->get('page'),
            'pagesize' => $request->get('limit')
        ];
        $res = $this->materialModel->getList($params);
        $data = [
            'code' => 0,
            'msg' => '正在请求中...',
            'count' => $res['total'],
            'title' => !empty($res['row']['title']) ? $res['row']['title'] : '',
            'data' => $res['row']
        ];
        return response()->json($data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function transfer(Request $request){
        $params = [
            'where' => [['is_delete', 1]],
            'getfield' => false,
            'order' => 'created_at',
            'field' => ['id', 'title'],
        ];
        $res = $this->materialModel->getList($params);

        $data = [
            'code' => 0,
            'msg' => '正在请求中...',
            'data' =>$res['row']
        ];
        return response()->json($data);
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add()
    {

        return view('admin.material.add', [
            'treeCat' => $this->downCatTree(),
            'materialTag' =>materialTag(),
            'sn_code'=>create_code(),
            'materialMode'=>materialMode(),
            'materialAuthMothod'=>materialAuthMothod(),
            'materialVersion'=>materialVersion(),
            'materialReferr'=>materialReferr(),
            'countryInFirst'=>countryInFirst()
        ]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addSave(Request $request){
        $data = $request->input();
        unset($data['_token']);
        unset($data['s']);
        $rs = $this->materialModel->add($data);
        if($rs){
            return response()->json(array('code'=>200,'msg'=>'保存成功'));
        }else{
            return response()->json(array('code'=>300,'msg'=>'保存失败'));
        }
    }

    /**
     * @param Request $request
     * @param $arg1
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function edit(Request $request,$arg1){
        $id =  $request->route('id');
        if(empty($id) || !is_numeric($id)){
            return response()->json(array('code'=>300,'msg'=>"参数为空"));exit;
        }
        $params = [
            'where' => [['id', $id]]

        ];
        $rs = $this->materialModel->findOne($params);
        $img_parans = [
            'where' => [['id', $rs->images]],
            'field' => ['id', 'img_url'],
            ];
        $rsa = $this->imagesModel->findOne($img_parans);
        $rs->images_url = $rsa->img_url;
        return view('admin.material.edit',[
            'treeCat' => $this->downCatTree(),
            'materialTag' =>materialTag(),
            'sn_code'=>create_code(),
            'materialMode'=>materialMode(),
            'materialAuthMothod'=>materialAuthMothod(),
            'materialVersion'=>materialVersion(),
            'materialReferr'=>materialReferr(),
            'countryInFirst'=>countryInFirst(),
            'result'=>objectToArray($rs)
        ]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editSave(Request $request){
        $params = [
            'where'=>[['id',$request->input('id')]],
            'field'=>[
                'title'             =>$request->input('title'),
                'serial'            =>$request->input('serial'),
                'downloadcat_id'    =>$request->input('downloadcat_id'),
                'key_seo'           =>$request->input('key_seo'),
                'key_description'   =>$request->input('key_description'),
                'info'              =>$request->input('info'),
                'content'           =>$request->input('content'),
                'price'             =>$request->input('price'),
                'pattern'           =>$request->input('pattern'),
                'author'            =>$request->input('author'),
                'source'            =>$request->input('source'),
                'is_check'          =>$request->input('is_check'),
                'grant_method'      =>$request->input('grant_method'),
                'is_hot'            =>$request->input('is_hot'),
                'is_first'          =>$request->input('is_first'),
                'images'            =>$request->input('images'),
                'updated_at'        =>date('Y-m-d H:i:s',time())
            ]
        ];
        $res = $this->materialModel->updateData($params);
        if($res){
            return response()->json(array('code'=>200,'msg'=>'编辑成功'));
        }else{
            return response()->json(array('code'=>300,'msg'=>'编辑失败'));
        }

    }
    public function del(Request $request){
        $id = $request->input('id');
        if(empty($id) || !is_numeric($id)){
            return response()->json(array('code'=>300,'msg'=>"参数为空"));exit;
        }
        $arr = [
            'where'=>[['id',$id]],
            'field'=>['is_delete'=>2]
        ];
        $res = $this->materialModel->updateData($arr);
        if($res){
            return response()->json(array('code'=>200,'msg'=>'删除成功'));
        }else{
            return response()->json(array('code'=>300,'msg'=>'删除失败'));
        }
    }
    /**
     *
     */
    private function downCatTree()
    {
        $params = [
            'where' => [['is_delete', 1], ['status', 1]],
            'field' => ['id', 'pid', 'title'],
            'getfields' => false,
            'order' => 'created_at'
        ];
        $res = $this->downCatModel->getList($params);
        return treeNode(objectToArray($res['row']));
    }
}
